package com.chuangke.poi;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class WordToPDFConverter {
	public static void main(String[] args) {
		try {
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

			
			Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
			
			// Step 1: Open the Word document as a plain text file
			FileInputStream stream = new FileInputStream("d:/text/b.docx");
			InputStreamReader reader = new InputStreamReader(stream);
			BufferedReader buffer = new BufferedReader(reader);
			// Step 2: Create a new PDF document using iTextPDF
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream("d:/text/a.pdf"));
			// Step 3: Open the document
			document.open();
			// Step 4: Read the text from the Word document and add it to the PDF document
			String line;
			while ((line = buffer.readLine()) != null) {
				Paragraph paragraph = new Paragraph(line, fontChinese) ;
//				paragraph.add(new Chunk(line));
				document.add(paragraph);
//				document.add(new Paragraph("中文", fontChinese));
			}
			// Step 5: Close the document
			document.close();
			// Step 6: Close the stream and reader
			buffer.close();
			reader.close();
			stream.close();
			System.out.println("Conversion successful!");
		} catch (IOException | DocumentException e) {
			e.printStackTrace();
			System.out.println("Conversion unsuccessful.");
		}
	}
}